<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D翻转轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f5f5f5;
        }
        .carousel {
            position: relative;
            width: 100vw;
            height: 400px;
            perspective: 1000px;
            border-radius: 10px;
            /*box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);*/
        }
        .carousel-inner {
            width: 100%;
            height: 100%;
            position: relative;
            transform-style: preserve-3d;
            transition: transform 1s;
        }
        .carousel-item {
            position: absolute;
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 48px;
            color: white;
            font-weight: bold;
            backface-visibility: hidden;
            transform-style: preserve-3d;
            transition: transform 1s;
        }
        .carousel-item:nth-child(1) { background-color: #000000; }
        .carousel-item:nth-child(2) { background-color: #000000; }
        .carousel-item:nth-child(3) { background-color: #000000; }
        .carousel-control {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 50px;
            height: 50px;
            background-color: rgba(0, 0, 0, 0.3);
            color: white;
            border: none;
            border-radius: 50%;
            font-size: 24px;
            cursor: pointer;
            z-index: 10;
        }
        .carousel-control.prev {
            left: 20px;
        }
        .carousel-control.next {
            right: 20px;
        }
        .carousel-indicators {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            gap: 10px;
            z-index: 10;
        }
        .carousel-indicator {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.5);
            cursor: pointer;
        }
        .carousel-indicator.active {
            background-color: white;
        }
    </style>
</head>
<body>
<div class="carousel">
    <div class="carousel-inner">
        <div class="carousel-item">1</div>
        <div class="carousel-item">2</div>
        <div class="carousel-item">3</div>
    </div>
    <button class="carousel-control prev">❮</button>
    <button class="carousel-control next">❯</button>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const carouselInner = document.querySelector('.carousel-inner');
        const items = document.querySelectorAll('.carousel-item');
        const prevBtn = document.querySelector('.prev');
        const nextBtn = document.querySelector('.next');
        const indicators = document.querySelectorAll('.carousel-indicator');
        let currentIndex = 0;
        let timer;
        const totalItems = items.length;
        const angle = 360 / totalItems;

        // 初始化3D布局
        function init3DLayout() {
            items.forEach((item, index) => {
                const rotateY = index * angle;
                item.style.transform = `rotateY(${rotateY}deg) translateZ(300px)`;
            });
        }

        // 更新轮播位置和指示器状态
        function updateCarousel() {
            const rotateY = -currentIndex * angle;
            carouselInner.style.transform = `rotateY(${rotateY}deg)`;
            indicators.forEach((indicator, index) => {
                indicator.classList.toggle('active', index === currentIndex);
            });
        }

        // 切换到下一张
        function nextSlide() {
            currentIndex = (currentIndex + 1) % totalItems;
            updateCarousel();
        }

        // 切换到上一张
        function prevSlide() {
            currentIndex = (currentIndex - 1 + totalItems) % totalItems;
            updateCarousel();
        }

        // 自动播放
        function startAutoPlay() {
            timer = setInterval(nextSlide, 3000);
        }

        // 暂停自动播放
        function pauseAutoPlay() {
            clearInterval(timer);
        }

        // 绑定事件
        nextBtn.addEventListener('click', () => {
            nextSlide();
            pauseAutoPlay();
        });
        prevBtn.addEventListener('click', () => {
            prevSlide();
            pauseAutoPlay();
        });
        indicators.forEach((indicator, index) => {
            indicator.addEventListener('click', () => {
                currentIndex = index;
                updateCarousel();
                pauseAutoPlay();
            });
        });

        // 鼠标悬停暂停
        document.querySelector('.carousel').addEventListener('mouseenter', pauseAutoPlay);
        document.querySelector('.carousel').addEventListener('mouseleave', startAutoPlay);

        // 初始化
        init3DLayout();
        // startAutoPlay();
    });
</script>
</body>
</html>
